const { sequelize } = require('../utils/database');

async function createMissingTables() {
  try {
    console.log('开始创建缺失的表...');
    
    // 创建用户关注表
    await sequelize.query(`
      CREATE TABLE IF NOT EXISTS user_follows (
        id INT AUTO_INCREMENT PRIMARY KEY,
        follower_id INT NOT NULL COMMENT '关注者ID',
        following_id INT NOT NULL COMMENT '被关注者ID',
        status ENUM('active', 'blocked') DEFAULT 'active' COMMENT '关注状态',
        createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX idx_follower_id (follower_id),
        INDEX idx_following_id (following_id),
        UNIQUE KEY unique_follow (follower_id, following_id),
        FOREIGN KEY (follower_id) REFERENCES users(id) ON DELETE CASCADE,
        FOREIGN KEY (following_id) REFERENCES users(id) ON DELETE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);
    console.log('✓ user_follows 表创建成功');

    // 创建动态点赞表
    await sequelize.query(`
      CREATE TABLE IF NOT EXISTS feed_likes (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL COMMENT '点赞用户ID',
        feed_id INT NOT NULL COMMENT '动态ID',
        createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_feed_id (feed_id),
        UNIQUE KEY unique_like (user_id, feed_id),
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
        FOREIGN KEY (feed_id) REFERENCES activity_feeds(id) ON DELETE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);
    console.log('✓ feed_likes 表创建成功');

    // 创建动态评论表
    await sequelize.query(`
      CREATE TABLE IF NOT EXISTS feed_comments (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL COMMENT '评论用户ID',
        feed_id INT NOT NULL COMMENT '动态ID',
        content TEXT NOT NULL COMMENT '评论内容',
        parent_id INT NULL COMMENT '父评论ID，用于回复功能',
        likes_count INT DEFAULT 0 COMMENT '点赞数量',
        is_deleted BOOLEAN DEFAULT FALSE COMMENT '是否已删除',
        createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX idx_user_id (user_id),
        INDEX idx_feed_id (feed_id),
        INDEX idx_parent_id (parent_id),
        INDEX idx_created_at (createdAt),
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
        FOREIGN KEY (feed_id) REFERENCES activity_feeds(id) ON DELETE CASCADE,
        FOREIGN KEY (parent_id) REFERENCES feed_comments(id) ON DELETE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    `);
    console.log('✓ feed_comments 表创建成功');

    console.log('所有缺失的表已创建完成！');
  } catch (error) {
    console.error('创建表失败:', error);
    throw error;
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  createMissingTables()
    .then(() => {
      console.log('表创建脚本执行完成');
      process.exit(0);
    })
    .catch(error => {
      console.error('表创建脚本执行失败:', error);
      process.exit(1);
    });
}

module.exports = { createMissingTables }; 